//
//  ZCLoginViewModel.m
//  ZCJZ
//
//  Created by sujeking on 2019/2/25.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCLoginViewModel.h"
#import <GTSDK/GeTuiSdk.h>
#import "ZCWebVC.h"
#import "AppAndDeviceTool.h"
#import <MJExtension.h>
#import "SKBaseWebService.h"
@implementation ZCLoginViewModel

+ (void)sendVirfiyCodeWithPhoneNum:(NSString *)phoneNum
                              type:(NSInteger)type
                          complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:phoneNum forKey:@"phone"];
    [parameters setValue:@(type) forKey:@"type"];
    [SKBaseWebService getRequest:@"/v1/member/sms" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

/// 用户名、密码登录
+ (void)loginServerWithUserName:(NSString *)userName
                       passWord:(NSString *)passWord
                       complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {

    AppAndDeviceTool *tool = [AppAndDeviceTool shareInstance];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:userName forKey:@"account"];
    [parameters setValue:passWord forKey:@"password"];
    
    [parameters setValue:tool.appVersionStr forKey:@"appVersion"];
    [parameters setValue:tool.appNameStr forKey:@"appName"];
    [parameters setValue:@"ios" forKey:@"systemType"];
    [parameters setValue:tool.deviceNameStr forKey:@"deviceName"];
    [parameters setValue:tool.connectionTypeStr forKey:@"connectionType"];
    [parameters setValue:tool.deviceIdStr forKey:@"deviceId"];
    [parameters setValue:tool.deviceModelStr forKey:@"deviceModel"];
    [parameters setValue:tool.deviceSystemVersionStr forKey:@"systemVersion"];
    [parameters setValue:@"iPhone" forKey:@"uiModel"];
    [parameters setValue:tool.channelStr forKey:@"channel"];
    [parameters setValue:tool.operatorStr forKey:@"operator"];
    [parameters setValue:tool.deviceTokenStr forKey:@"deviceToken"];
    
    
    [SKBaseWebService postRequest:@"/v1/member/login" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        if (data.allKeys.count == 0) {
            block(status,msg,nil);
        } else {
            ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
            ZCBaseUserModel *userModel = [ZCBaseUserModel mj_objectWithKeyValues:data];
            manager.userModel = userModel;
            [GeTuiSdk bindAlias:userModel.member.idstr andSequenceNum:userModel.member.idstr];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
                [ud setValue:userModel.mj_keyValues forKey:@"userinfo"];
                [ud setValue:userModel.member.accessToken forKey:@"accessToken"];
                [ud synchronize];
            });
            block(status,msg,data);
        }
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)registAcountServerWithInfo:(NSMutableDictionary *)info
                          complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {

    NSMutableDictionary *parameters = info;
    
    [SKBaseWebService postRequest:@"/v1/third/app-register-new" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];

}

+ (void)resetPasswordWithInfo:(NSMutableDictionary *)info
                     complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {

    NSMutableDictionary *parameters = info;
    [SKBaseWebService postRequest:@"/v1/setting/reset-password" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}


+ (void)fetchShoppingCarNumWithComplete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [SKBaseWebService getRequest:@"/v1/member/cart-num" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"mainshoppingnum" object:data];
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

/// 进入微折购用户注册协议
+ (void)loadFileDesOneVC {
    ZCWebVC *vc = [[ZCWebVC alloc] init];
    extern NSString *baseURLStr;
    vc.remotePath = @"http://shaoyue.ec-cloudtech.com/#/protocol/register";
    vc.navTitle = @"注册协议";
    [[self visibleViewController].navigationController pushViewController:vc animated:YES];
}

/// 微折购隐私政策
+ (void)loadFileDesTwoVC {
    ZCWebVC *vc = [[ZCWebVC alloc] init];
    vc.remotePath = @"http://shaoyue.ec-cloudtech.com/#/protocol/protocol1";
    vc.navTitle = @"注册协议";
    [[self visibleViewController].navigationController pushViewController:vc animated:YES];
}


+ (UIViewController *)visibleViewController {
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;;
    return [self getVisibleViewControllerFrom:rootViewController];
}

+ (UIViewController *)getVisibleViewControllerFrom:(UIViewController *) vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [self getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}


@end
